//
//  ONEChatMeTableViewController.m
//  ONEChat
//
//  Created by 楼楚 on 15/12/17.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "ONEChatMeTableViewController.h"
#import "XMPPvCardTemp.h"
@interface ONEChatMeTableViewController()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
/*
 头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
/*
 昵称
 */
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;
/*
 账号
 */
@property (weak, nonatomic) IBOutlet UILabel *chatnameLabel;



@end


@implementation ONEChatMeTableViewController
- (IBAction)logoutClick {
    
    
    [[ONEChatXMPPTool sharedONEChatXMPPTool]xmppUserlogout];
    
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"我";
    
    XMPPvCardTemp *myVCard = [ONEChatXMPPTool sharedONEChatXMPPTool].vCard.myvCardTemp;
    if (myVCard.photo) {
        self.avatarView.image = [UIImage imageWithData:myVCard.photo];
       
    }
    
    self.nicknameLabel.text = myVCard.nickname;
   
    
    NSString *user = [ONEChatUserInfo sharedONEChatUserInfo].user;
    
    self.chatnameLabel.text = [NSString stringWithFormat:@"%@",user];
    

    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取cell的tag
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSInteger *tag = cell.tag;
    //    判断
    if (tag == 0) {
        //        选择照片
        UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相册" otherButtonTitles:nil, nil];
        [sheet showInView:self.view];
        NSLog(@"这行放图片");
    }
    if (tag == 1){
        //        跳到下一个控制器
        NSLog(@"这行是昵称");
    }else{
        //        什么都不干
        NSLog(@"这行是账号");
    }
}

#pragma mark - ActionSheet的代理
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    //   设置代理
    imagePicker.delegate = self;
    //    设置允许编辑
    imagePicker.allowsEditing = YES;
    
    if (buttonIndex == 0) {
        //相册
        NSLog(@"相册");
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
    }
     else{
        //取消
        return;
    }
    //    显示图片选择器
    [self presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark - 图片选择器的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"%@",info);
    //    获取图片 设置图片
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    self.avatarView.image = image;
       //    隐藏窗口
    [self dismissViewControllerAnimated:YES completion:nil];
    XMPPvCardTemp *myvCard = [ONEChatXMPPTool sharedONEChatXMPPTool].vCard.myvCardTemp;
    
    myvCard.photo =UIImagePNGRepresentation(image);
    
    [[ONEChatXMPPTool sharedONEChatXMPPTool].vCard updateMyvCardTemp:myvCard];
    
}
@end
